package com.kangtushizhe.utils;

import com.kangtushizhe.emuns.PermissionDTO;
import com.kangtushizhe.emuns.RoleDTO;
import com.kangtushizhe.service.UserRoleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class AuthoritiesUtil {
    @Resource
    private UserRoleService userRoleService;

    public List<String> getAuthoritiesByUserId(Integer userId) {
        //            查询角色 权限 集合
        List<RoleDTO> rolesList = userRoleService.getRolesByUserId(userId);
        List<PermissionDTO> permissionList = userRoleService.getPermissionsByUserId(userId);
        List<String> authorities = new ArrayList<>();
        for (RoleDTO role : rolesList) {
            //          角色名称纳入的权限管理，必须增加前缀ROLE_
            authorities.add("ROLE_" + role.getName());
        }
        for (PermissionDTO permission : permissionList) {
            //          权限字符串描述，不需要特定的任何前后缀。
            authorities.add(permission.getPermit());
        }
        return authorities;
    }
}
